﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NAudio.Wave;
using System.IO;

namespace PlaySoundActionPlugin
{
    /// <summary>
    /// Interaction logic for PlaySoundConfigPanel.xaml
    /// </summary>
    public partial class PlaySoundActionParamsPanel : UserControl
    {
        public PlaySoundActionParamsPanel()
        {
            InitializeComponent();
        }

        private void findFile_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = ""; // Default file name
            dlg.Multiselect = false;
            dlg.DefaultExt = ".mp3"; // Default file extension
            dlg.Filter = "Audio Files(*.MP3;*.WMA;*.WAV)|*.MP3;*.WMA;*.WAV|All files (*.*)|*.*"; // Filter files by extension

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                PlaySoundActionParameters model = (PlaySoundActionParameters)DataContext;
                model.PathToFile =dlg.FileName;
            }
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            PlaySoundActionParameters model = (PlaySoundActionParameters)DataContext;
            //model.StartRecordig();
        }

        private void Grid_Unloaded(object sender, RoutedEventArgs e)
        {
            PlaySoundActionParameters model = (PlaySoundActionParameters)DataContext;
            model.StopRecording();
        }

        private void deviceChoose_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PlaySoundActionParameters model = (PlaySoundActionParameters)DataContext;
            model.StopRecording();
            model.StartRecordig();
        }

        private void record_Click(object sender, RoutedEventArgs e)
        {
            PlaySoundActionParameters model = (PlaySoundActionParameters)DataContext;
            if (model.Recording == true)
            {
                model.Recording = false;
                model.Writer.Flush();
                model.Writer.Close();
            }
            else {
                model.InitWriter();
                model.Recording = true;
            }
            
        }

        private void reset_Click(object sender, RoutedEventArgs e)
        {
            PlaySoundActionParameters model = (PlaySoundActionParameters)DataContext;
            if (model.Recording == true)
            {
                model.Recording = false;
            }
            if (model.Writer != null)
            {
                WaveFileWriter tmp = model.Writer;
                model.Writer = null;
                model.SampleAggregator.RaiseRestart();
                string name = tmp.Filename;
                tmp.Close();
                tmp.Dispose();
                File.Delete(name);
            }
        }

        private void play_Click(object sender, RoutedEventArgs e)
        { 
            PlaySoundActionParameters model = (PlaySoundActionParameters)DataContext;
            if (model.Recording == false)
            {
                PlaySoundActionPlugin.Open(model.SoundFile);
                PlaySoundActionPlugin.Play(false);
            }

        }

        private void stop_Click(object sender, RoutedEventArgs e)
        {
            PlaySoundActionPlugin.Close();
        }

        private void get_Click(object sender, RoutedEventArgs e)
        {
            PlaySoundActionParameters model = (PlaySoundActionParameters)DataContext;
            model.LoadGoogleTTS(model.TtsText, model.SelectedLang);
        }
    }
}
